package com.play.ballen.network

import androidx.appcompat.app.AppCompatDelegate
import com.play.ballen.R
import com.play.ballen.utils.UserInfoManager
import com.yariksoffice.lingver.Lingver
import com.yuven.appframework.util.DeviceIdUtil
import com.yuven.baselib.utils.Logger
import com.yuven.baselib.utils.ResourceUtil
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import java.io.IOException
import java.util.*

class BallenHeaderInterceptor : Interceptor {

    private var language = ""

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val original: Request = chain.request()

        var token =UserInfoManager.getUserToken()


        language = Lingver.getInstance().getLanguage()
        Logger.i("BallenHeaderInterceptor.token = $token")

        val requestBuilder: Request.Builder = original.newBuilder()
            .addHeader("X-Requested-With", "XMLHttpRequest")
            .addHeader("Accept-Language", language)
            .addHeader("Authorization", token?:"")
        val request: Request = requestBuilder.build()
        return chain.proceed(request)
    }

    private fun getLanguage(): String {
        return Lingver.getInstance().getLanguage()
        /*val localeListCompat = AppCompatDelegate.getApplicationLocales()
        return if (localeListCompat.isEmpty) {
            DeviceIdUtil.getSystemLanguage()
        } else {
            localeListCompat.toLanguageTags()
        }*/
    }

}